<?php

namespace app\model;

use think\Model;
use think\facade\Request;
use app\model\MemberBuyer;

class BuyerExchange extends Model
{
    protected $pk = 'id';

    public $errorMsg = '';

    public function getTypeTextAttr($value, $data)
    {
        $status = [1 => '本金', 2 => '佣金'];
        return $status[$data['status']];
    }

    public function getAmountAttr($value){return sprintf("%.2f",$value);}
    public function getBalanceAttr($value){return sprintf("%.2f",$value);}
    public function getCommissionAttr($value){return sprintf("%.2f",$value);}

    public static function getExchange($mp = [])
    {
        $where = [];

        if (!empty($mp['userid'])) {
            $where[] = ['userid', '=', $mp['userid']];
        }

        if (!empty($mp['currencytype'])) {
            $where[] = ['currencytype', '=', $mp['currencytype']];
        }

        if (!empty($mp['stime']) && !empty($mp['etime'])) {
            $where[] = ['addtime', '>=', $mp['stime']];
            $where[] = ['addtime', '<=', $mp['etime']];
        }

        return self::where($where)->order('addtime', 'desc')->paginate(20);
    }

    public function increaseAmount($userid, $amount, $info)
    {
        $memberAmount = MemberBuyer::where('userid', $userid)->value('amount');
        MemberBuyer::where('userid', $userid)->setInc('amount', $amount);
        self::save([
            'currencytype' => 1,
            'amount' => $amount,
            'perbalance' => $memberAmount,
            'balance' => $memberAmount + $amount,
            'userid' => $userid,
            'ip' => Request::ip(),
            'note' => $info,
        ]);

        return true;
    }

    public function reduceAmount($userid, $amount, $info)
    {
        $memberAmount = MemberBuyer::where('userid', $userid)->value('amount');
        if ($memberAmount < $amount) {
            $this->errorMsg = '本金不足';
            return false;
        }

        MemberBuyer::where('userid', $userid)->setDec('amount', $amount);
        self::save([
            'currencytype' => 1,
            'amount' => -$amount,
            'perbalance' => $memberAmount,
            'balance' => $memberAmount - $amount,
            'userid' => $userid,
            'ip' => Request::ip(),
            'note' => $info,
        ]);

        return true;
    }

    public function increaseCoin($userid, $coin, $info)
    {
        $memberCoin = MemberBuyer::where('userid', $userid)->value('coin');
        MemberBuyer::where('userid', $userid)->setInc('coin', $coin);
        self::save([
            'currencytype' => 2,
            'amount' => $coin,
            'perbalance' => $memberCoin,
            'balance' => $memberCoin + $coin,
            'userid' => $userid,
            'ip' => Request::ip(),
            'note' => $info,
        ]);

        return true;
    }

    public function reduceCoin($userid, $coin, $info)
    {
        $memberCoin = MemberBuyer::where('userid', $userid)->value('coin');
        if ($memberCoin < $coin) {
            $this->errorMsg = '佣金不足';
            return false;
        }

        MemberBuyer::where('userid', $userid)->setDec('coin', $coin);
        self::save([
            'currencytype' => 2,
            'amount' => -$coin,
            'perbalance' => $memberCoin,
            'balance' => $memberCoin - $coin,
            'userid' => $userid,
            'ip' => Request::ip(),
            'note' => $info,
        ]);

        return true;
    }
}